package net.wds.ssbus.modules.ssbus.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import net.wds.ssbus.common.utils.R;
import net.wds.ssbus.modules.dict.entity.TbBaseDictitemEntity;
import net.wds.ssbus.modules.ssbus.pojo.TbSsbusChannelEntrty;
import net.wds.ssbus.modules.ssbus.service.TbSsbusChannelService;
import net.wds.ssbus.modules.ssbus.util.GetIpAndPortUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/tbssbuschannel")
public class TbSsbusChannelController {
    /**
    * @author zl
    * @date 2020/10/31
    */
    @Resource
    private TbSsbusChannelService tbSsbusChannelService;

    /**
     *
     * @param key 查询条件
     * @param page 分页数量
     * @param limit 分页大小
     * @return
     */
    @GetMapping("/list")
    public R getList(@RequestParam(required=false) String key,@RequestParam(required=false) String queryType, @RequestParam(value="page",defaultValue="0")int page, @RequestParam(value="limit",defaultValue="5")int limit) {
        IPage<TbSsbusChannelEntrty> page1=tbSsbusChannelService.list(page,limit,key,queryType);
        String ip= GetIpAndPortUtils.getLocalIP();
        return R.ok().put("page", page1);
    }

    /**
     *
     * @param tbSsbusChannelEntrty
     * @return
     * @throws IOException
     */
    @PostMapping("/connect")
    public R connect(@RequestBody TbSsbusChannelEntrty tbSsbusChannelEntrty){
       //boolean b= GetIpAndPort.connect(server,servPort);
        boolean b= GetIpAndPortUtils.testing(tbSsbusChannelEntrty);
        return R.ok().put("data",b);
    }

    /**
     *
     * @return 返回数据库类型
     */
    @GetMapping("/dbtype")
    public R getDbType() {
        List<TbBaseDictitemEntity> date=tbSsbusChannelService.dbtype();
        return R.ok().put("date", date);
    }

    /**
     *
     * @return 返回状态
     */
    @GetMapping("/active")
    public R active() {
        List<TbBaseDictitemEntity> date=tbSsbusChannelService.active();
        return R.ok().put("date", date);
    }

    /**
     *根据id修改状态值
     * @param id  通道管理的id
     * @param active  状态值
     * @return
     */
    @GetMapping("/updateActive")
    public R updateActive(@RequestParam Integer id,@RequestParam Integer active) {
        tbSsbusChannelService.updateActive(id,active);
        return R.ok();
    }

    /**
     *
     * @param tbSsbusChannelEntrty 更新或保存通道管理
     * @return
     */
    @PostMapping("/saveOrupdate")
    public R saveOrUpdate(@RequestBody TbSsbusChannelEntrty tbSsbusChannelEntrty) {
        tbSsbusChannelService.saveOrupdate(tbSsbusChannelEntrty);
        return R.ok();
    }

    /**
     *
     * @param id 根据id获取通道信息
     * @return
     */
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable("id") Long id) {
        TbSsbusChannelEntrty tbSsbusChannelEntrty = tbSsbusChannelService.getById(id);
        return R.ok().put("date",tbSsbusChannelEntrty);
    }

    /**
     *
     * @param id 根据id批量删除
     * @return
     */
    @ApiOperation(value = "", notes = "删除")
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] id) {
       tbSsbusChannelService.deletes(id);
        return R.ok();
    }

}
